package com.bjsxt.back.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.controller.BaseController;
import com.bjsxt.common.util.ConstatFinalUtil;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.investor.pojo.AInvestor;
import com.bjsxt.investor.service.IInvestorService;

/**
 * 所有出资相关的后台(使用者--管理员)操作
 * @author wangshSxt
 */
@Controller
@RequestMapping("/back/investor/")
public class InvestorBackController extends BaseController
{
	@Resource
	private IInvestorService investorService;
	/**
	 * 出资方列表
	 * @return
	 */
	@RequestMapping("/investorList.htm")
	public String investorList(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER_SYSTEM.info("--investorList--");
		/* 接收参数 */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		String coopType = request.getParameter("coopType");
		
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		
		Date stDate = null ; 
		Date edDate = null ; 
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateFormatUtil.parseDateTime(st);
			edDate = this.dateFormatUtil.parseDateTime(ed);
		}
		
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", keyword);
		condMap.put("status", status);
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		condMap.put("coopType", coopType);
		List<AInvestor> investorList = this.investorService.findCondListInvestorService(pageInfoUtil, condMap);
		
		/* 存储结果 */
		request.setAttribute("investorList", investorList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 存储条件 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		request.setAttribute("coopType", coopType);
		return "/back/investor/investorList" ; 
	}
	
	/**
	 * 打开更新页面
	 * @return
	 */
	@RequestMapping("/investorUpdate.htm")
	public String investorUpdate(String iid,HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER_SYSTEM.info("--investorUpdate--");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", iid);
		AInvestor investor = this.investorService.findOneInvestorService(condMap);
		request.setAttribute("investor", investor);
		return "/back/investor/investorUpdate" ; 
	}
	
	/**
	 * 更新提交
	 * 
	 * 更新的时候不建议使用对象属性驱动
	 * ~如果表中有30个字段;必须将所有的字段都写到表单中的name上;不写不赋值
	 * ~类型转换器;如果转换失败会报400错误
	 * 
	 * produces = "text/html;charset=UTF-8"
	 * response.setContextType("text/html;charset=UTF-8");
	 * @return
	 */
	@RequestMapping(value = "/investorUpdateSubmit.htm",produces = "text/html;charset=UTF-8")
	public @ResponseBody String investorUpdateSubmit(String id,HttpServletRequest request,MultipartFile file)
	{
		ConstatFinalUtil.LOGGER_SYSTEM.info("--investorUpdateSubmit--");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		AInvestor investor = this.investorService.findOneInvestorService(condMap);
		if(investor != null)
		{
			/* 接收参数 */
			String code = request.getParameter("code");
			String name = request.getParameter("name");
			String url = request.getParameter("url");
			String contactName = request.getParameter("contactName");
			String contactPhone = request.getParameter("contactPhone");
			String content = request.getParameter("content");
			String totalNum = request.getParameter("totalNum");
			String status = request.getParameter("status");
			String coopType = request.getParameter("coopType");
			
			String pubTime = request.getParameter("pubTime");
			investor.setPubTime(this.dateFormatUtil.parseDateTime(pubTime));
			
			investor.setCode(code);
			investor.setName(name);
			investor.setUrl(url);
			investor.setContactName(contactName);
			investor.setContactPhone(contactPhone);
			investor.setTotalNum(Integer.valueOf(totalNum));
			investor.setContent(content);
			investor.setCoopType(Byte.valueOf(coopType));
			investor.setStatus(Byte.valueOf(status));
			
			investor.setUpdateTime(new Date());
			/* 处理文件上传需要的信息 */
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			/* 上传文件 */
			paramsMap.put("file", file);
			/* tomcat真实路径 */
			paramsMap.put("truePath", request.getRealPath("/"));
			try
			{
				JSONObject resultDbJSON = this.investorService.updateOneInvestorService(investor,paramsMap);
				this.info = resultDbJSON.getString("info");
			} catch (IOException e)
			{
				this.info = "更新出资方方失败了" ;
			}
		}else
		{
			this.info = "当前出资方不存在" ;
		}
		JSONObject resultJSON = this.returnJuiJSON(request, info);
		return resultJSON.toJSONString() ;
	}
	
}
